package com.bmslz.monitor.helper;

import com.bmslz.monitor.base.CommonConstant;
import com.bmslz.monitor.base.util.PasswordUtil;
import com.bmslz.monitor.base.config.excep.MyException;
import com.bmslz.monitor.base.dto.LoginDto;
import com.bmslz.monitor.base.entity.User;
import com.bmslz.monitor.service.IUserService;
import com.bmslz.monitor.base.vo.LoginVo;
import com.bmslz.monitor.base.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

@Component
public class LoginHelper {

    private static final ThreadLocal<User> localToken = new ThreadLocal<>();
    private static final ThreadLocal<Boolean> closeUserIsolationTag = new ThreadLocal<>();

    @Autowired
    private IUserService userService;


    /**
     * 登录
     *
     * @param loginDto
     * @return loginVo
     */
    public LoginVo login(LoginDto loginDto) {
        loginDto.check();
        String userName = loginDto.getUserName();
        String password = loginDto.getPassword();
        User user = userService.queryUserByUsername(userName);
        if (user == null) {
            throw new MyException(500, "用户名不存在或不正确");
        }
        Date orderCloseTime = user.getOrderCloseTime();
        String role = user.getRole();
        if (!CommonConstant.ROLE_SUPER.equals(role)) {
            //判断用户套餐是否过期，过期不允许登录
            if (orderCloseTime == null || orderCloseTime.getTime() <= new Date().getTime()) {
                throw new MyException(500, "请联系管理员购买套餐包");
            }
        }
        String encryptPassword = PasswordUtil.encrypt(userName, password, user.getSalt());
        if (!encryptPassword.equals(user.getPassword())) {
            throw new MyException(500, "密码错误");
        }
        String token = JwtHelper.generateToken(userName);
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setRefreshToken(token);
        return loginVo;
    }

    /**
     * 获取登录用户信息
     *
     * @return
     */
    public UserInfoVo userInfo() {
        UserInfoVo userInfoVo = new UserInfoVo();
        //暂时无角色和菜单权限一说 写死配合前端框架即可
        userInfoVo.setRoles(new ArrayList<>(Collections.singletonList(LoginHelper.getUser().getRole())));
        userInfoVo.setButtons(new ArrayList<>(Arrays.asList("B_CODE1", "B_CODE2", "B_CODE3")));
        userInfoVo.setUserId(getUserId());
        userInfoVo.setUserName(getUserName());
        return userInfoVo;
    }

    /**
     * 设置线程user
     *
     * @param user
     */
    public static void setUser(User user) {
        localToken.set(user);
    }

    /**
     * 获取线程token
     *
     * @return
     */
    public static User getUser() {
        return localToken.get();
    }

    /**
     * 移除线程User
     */
    public static void remove() {
        localToken.remove();
    }

    /**
     * 获取当前登录用户id
     *
     * @return
     */
    public String getUserId() {
        return getUser().getId();
    }

    /**
     * 获取当前登录的用户名
     *
     * @return
     */
    public String getUserName() {
        return getUser().getUsername();
    }

    /**
     * 手动控制上下文 当前线程关闭用户数据隔离
     */
    public static void closeUserIsolation() {
        closeUserIsolationTag.set(true);
    }

    /**
     * 手动控制上下文 当前线程开启用户数据隔离
     */
    public static void openUserIsolation(User user) {
        closeUserIsolationTag.set(null);
        setUser(user);
    }

    public static Boolean getCloseIsolation() {
        return closeUserIsolationTag.get();
    }
}
